package com.sx.manage.controller.loan;

import java.io.IOException;

import javax.annotation.Resource;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.sx.common.util.json.JackJsonUtil;
import com.sx.manage.controller.base.MBaseController;
import com.sx.manage.service.loan.IMLoanService;
import com.sx.manage.servlet.ViewAndModel;
import com.sx.model.constants.MDataStatusType;
import com.sx.model.manage.mybatis.form.loan.MLoanForm;
import com.sx.model.manage.mybatis.vo.loan.MLoanVO;
import com.sx.model.manage.mybatis.vo.result.ResultBooleanVO;
import com.sx.model.manage.mybatis.vo.result.ResultListVO;
import com.sx.model.manage.mybatis.vo.result.ResultObjectVO;
import com.sx.model.shopping.mybatis.vo.goods.MGoodsInfoVO;
import com.sx.shopping.service.goods.MGoodsInfoWebService;

/**
 * 贷款基础数据
 * 
 * @author zwm
 *
 */
@Controller
@RequestMapping("/loan_info")
public class MLoanController extends MBaseController {

	private Logger _log = LoggerFactory.getLogger(this.getClass());

	@Resource(name = "mLoanServiceImpl")
	private IMLoanService mLoanServiceImpl;
	@Resource(name="goodsInfoWebService")
	private MGoodsInfoWebService goodsInfoWebServiceImpl ;
	/**
	 * 贷款基础数据 添加视图
	 * 
	 * @return
	 */
	@RequestMapping(value = "loan_index")
	public ViewAndModel add_view_emprunt_info() {

		ViewAndModel model = new ViewAndModel(request, cache);
		// 修改ftl页面名称
		model.setViewName("loan/addloan");
		return model;
	}

	/**
	 * 贷款基础数据 编辑
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 */
	@RequestMapping(value = "loan_info_eidt")
	@ResponseBody
	public ResultBooleanVO add_emprunt_info(MLoanForm form){
		_log.debug("添加系统操作日志：MLoanController===add_emprunt_info==request_message=" + form);
		if (null == form) {
			return new ResultBooleanVO("sj.m.common.add.error");
		}
		if (null != form.getId()) {
			return this.mLoanServiceImpl.edit(form, loginConsumer());
		}
		return this.mLoanServiceImpl.add(form, loginConsumer());

	}

	/**
	 * 贷款基础数据 查询
	 * 
	 * @param form
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 */
	@RequestMapping("/query_byid/{id}")
	@ResponseBody
	public ViewAndModel queryBy_emprunt_info(MLoanForm form){
		_log.debug("添加系统操作日志：MLoanController===queryBy_emprunt_info==formJson=" + JackJsonUtil.objToJson(form));
		ViewAndModel model = new ViewAndModel(request, cache);
		// 查询类
		ResultObjectVO<MLoanVO> result = mLoanServiceImpl.queryById(form);
		
		MGoodsInfoVO goodsVO = goodsInfoWebServiceImpl.queryById(result.getObjVo().getProduct_id()).getObjVo();
		MLoanVO vo = result.getObjVo();
		model.addObject("loanVO", vo);
		model.addObject("goodsVO", goodsVO);
		model.setViewName("loan/loanedit");// 修改ftl页面名称
		return model;
	}

	/**
	 * 贷款基础数据 查询
	 * 
	 * @param form
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 */
	@RequestMapping("/query_view/{id}")
	@ResponseBody
	public ViewAndModel queryBy_emprunt_view_info(MLoanForm form){
		_log.debug("添加系统操作日志：MLoanController===queryBy_emprunt_view_info==formJson=" + JackJsonUtil.objToJson(form));
		ViewAndModel model = new ViewAndModel(request, cache);
		// 查询类
		ResultObjectVO<MLoanVO> result = mLoanServiceImpl.queryById(form);
		MLoanVO vo = result.getObjVo();
		model.addObject("loanVO", vo);
		model.setViewName("loan/loanview");// 修改ftl页面名称
		
		
		return model;
	}

	/**
	 * 贷款基础数据-查询列表视图
	 * 
	 * @return
	 */
	@RequestMapping("/loan_view")
	public ViewAndModel search_emprunt_info() {
		ViewAndModel model = new ViewAndModel(request, cache);
		// 如需在添加页需要加载元素在此处增加 
		model.setViewName("loan/personLoan");// 修改fitly页面名称
		return model;
	}

	/**
	 * 贷款基础数据 数据集合
	 * 
	 * @param form
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 */
	@RequestMapping("/loan_page")
	@ResponseBody
	public ResultListVO<MLoanVO> search_emprunt_info(MLoanForm form){
		_log.debug("添加系统操作日志：MLoanController===search_emprunt_info==formJson=" + JackJsonUtil.objToJson(form));
		if ("".equals(form.getBusiness_name())) {
			form.setBusiness_name(null);
		}
		if ("".equals(form.getProduct_type())) {
			form.setProduct_type(null);
		}
		if ("".equals(form.getMax_interest_rate())) {
			form.setMax_interest_rate(null);
		}
		if ("".equals(form.getAddress())) {
			form.setAddress(null);
		}
		return this.mLoanServiceImpl.queryListByForm(form);
	}

	/**
	 * 银行状态维护-启用/停用
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 */
	@RequestMapping("/loan_update_status")
	@ResponseBody
	public ResultBooleanVO update_status(HttpServletRequest request, HttpServletResponse response){
		Long id = Long.valueOf(request.getParameter("id"));
		String status = request.getParameter("status");
		_log.debug("添加系统操作日志：MLoanController===search_emprunt_info==id=" + id + "status:" + status);
		if (0 == id) {
			return new ResultBooleanVO("sj.m.common.update.error");
		}
		if ("".equals(status)) {
			return new ResultBooleanVO("sj.m.common.update.error");
		}
		MLoanForm form = new MLoanForm();
		form.setId(id);
		if (status.equals(MDataStatusType.DATA_AVAILABLE.toString())) {
			form.setStatus(MDataStatusType.DATA_AVAILABLE);
		} else if (status.equals(MDataStatusType.DATA_LOCKING.toString())) {
			form.setStatus(MDataStatusType.DATA_LOCKING);
		}
		return mLoanServiceImpl.edit(form, loginConsumer());
	}

}
